import redis
class MyRedis:
    def __init__(self) -> None:
        # 创建 Redis 连接池
        pool = redis.ConnectionPool(host='localhost', port=6379, db=0, max_connections=10)
        # 使用连接池创建 Redis 连接对象
        self.r = redis.Redis(connection_pool=pool)

        # 字符串添加

    def str_set(self, key, value):
        return self.r.set(key, value)
    # 加过期时间
    def str_set_exp(self, key, time, value):
        return self.r.setex(key, time, value)

    # 字符串获取
    def str_get(self, key):
        value = self.r.get(key)
        if value:
            value = value.decode()
        return value

    # 分步式锁
    def setnx_str(self, key, value):
        return self.r.setnx(name=key, value=value)

    # 删除key
    def key_del(self, key):
        return self.r.delete(key)

    def list_add(self, key, value):
        return self.r.lpush(key, value)

    def list_all(self, key):
        data = self.r.lrange(key, 0, -1)
        return data

    def list_pop(self, key):
        return self.r.rpop(key)

    def list_len(self, key):
        return self.r.llen(key)

    def list_delvalue(self, key, value):
        return self.r.lrem(key, 0, value)

    def int_jian(self, key):
        return self.r.decr(key)

r = MyRedis()
# print(r.setnx_str('abcd22111222','123aaa'))
# # print(r.str_setnx('a11','456'))
# print(r.str_get('aert3tdgdet32r211'))
# r.str_set('abc','124234')
# print(r.str_get('abc3'))